//
//  UIView+Externsion.m
//  SinaCar
//
//  Created by Buddha on 15/12/2.
//  Copyright © 2015年 sina.com. All rights reserved.
//

#import "UIView+Externsion.h"

@implementation UIView (Externsion)
// 一圈 阴影
- (void)setCornerRadius:(CGFloat)radius withShadow:(BOOL)shadow withOpacity:(CGFloat)opacity {
    self.layer.cornerRadius = radius;
    if (shadow) {
        self.layer.shadowColor = [UIColor greenColor].CGColor;
        self.layer.shadowOpacity = opacity;
        self.layer.shadowOffset = CGSizeMake(0, 2);
        self.layer.shadowRadius = 4;
        self.layer.shouldRasterize = NO;
        self.layer.shadowPath = [[UIBezierPath bezierPathWithRoundedRect:[self bounds] cornerRadius:radius] CGPath];
    }
    self.layer.masksToBounds = !shadow;
}



- (void)setX:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (CGFloat)x
{
    return self.frame.origin.x;
}

- (void)setY:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)y
{
    return self.frame.origin.y;
}

- (void)setCenterX:(CGFloat)centerX
{
    CGPoint center = self.center;
    center.x = centerX;
    self.center = center;
}

- (CGFloat)centerX
{
    return self.center.x;
}

- (void)setCenterY:(CGFloat)centerY
{
    CGPoint center = self.center;
    center.y = centerY;
    self.center = center;
}

- (CGFloat)centerY
{
    return self.center.y;
}

- (void)setWidth:(CGFloat)width
{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (CGFloat)width
{
    return self.frame.size.width;
}

- (void)setHeight:(CGFloat)height
{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)height
{
    return self.frame.size.height;
}

- (void)setSize:(CGSize)size
{
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (CGSize)size
{
    return self.frame.size;
}

- (CGFloat) top
{
    return self.frame.origin.y;
}

- (void) setTop: (CGFloat) newtop
{
    CGRect newframe = self.frame;
    newframe.origin.y = newtop;
    self.frame = newframe;
}

- (CGFloat) left
{
    return self.frame.origin.x;
}

- (void) setLeft: (CGFloat) newleft
{
    CGRect newframe = self.frame;
    newframe.origin.x = newleft;
    self.frame = newframe;
}

- (CGFloat) bottom
{
    return self.frame.origin.y + self.frame.size.height;
}

- (void) setBottom: (CGFloat) newbottom
{
    CGRect newframe = self.frame;
    newframe.origin.y = newbottom - self.frame.size.height;
    self.frame = newframe;
}

- (CGFloat) right
{
    return self.frame.origin.x + self.frame.size.width;
}

- (void) setRight: (CGFloat) newright
{
    CGFloat delta = newright - (self.frame.origin.x + self.frame.size.width);
    CGRect newframe = self.frame;
    newframe.origin.x += delta ;
    self.frame = newframe;
}


- (UIViewController *)getVC{
    UIResponder *nextRes = [self nextResponder];
    do {
        if ([nextRes isKindOfClass:[UIViewController class]]) {
            return (UIViewController *)nextRes;
        }
        nextRes = [nextRes nextResponder];
    } while (nextRes != nil);
    
    return nil;
}


- (void)setStrId:(NSString *)strId{
    objc_setAssociatedObject(self, "strId", strId, OBJC_ASSOCIATION_COPY);
}
- (NSString *)strId{
    return objc_getAssociatedObject(self, "strId");
}


+ (void)load{
    
    Method oldM = class_getInstanceMethod([self class], @selector(addSubview:));
    Method newM = class_getInstanceMethod([self class], @selector(xs_addSubview:));
    
    
    BOOL isS = class_addMethod([self class], @selector(addSubview:), method_getImplementation(newM), method_getTypeEncoding(newM));
    if (isS) {
        class_replaceMethod([self class], @selector(xs_addSubview:), method_getImplementation(oldM), method_getTypeEncoding(oldM));
    }else{
        method_exchangeImplementations(oldM, newM);
    }

    
    
}
- (void)xs_addSubview:(UIView *)view{
    
    [@"has" hash];
    if (view.strId == @"a") {
        view.backgroundColor = [UIColor yellowColor];

    }
    
    [self xs_addSubview:view];
}

- (UIView*)subViewOfClassName:(NSString*)className {
    for (UIView* subView in self.subviews) {
        if ([NSStringFromClass(subView.class) isEqualToString:className]) {
            return subView;
        }
        
        UIView* resultFound = [subView subViewOfClassName:className];
        if (resultFound) {
            return resultFound;
        }
    }
    return nil;
}





- (UIImage*) changeImage{
    if(UIGraphicsBeginImageContextWithOptions)
    {
        UIGraphicsBeginImageContextWithOptions(self.frame.size, YES, 0.0);
    } else {
        UIGraphicsBeginImageContext(self.frame.size);
    }
    if ([[UIDevice currentDevice].systemVersion floatValue] >= 7.0) {
        [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES];
    }else{
        [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    }
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}





@end
